<?php

class JqGridDataColumn extends CComponent {

    /**
     * @var JqGridView the grid view object that owns this column.
     */
    public $grid;
    /**
     * @var string the attribute name of the data model. 
     */
    public $name;
    /**
     * @var string alignment of the cell in the Body layer, not in header cell.
     * Possible values: left, center, right.
     */
    public $align = 'left';
    /**
     * @var bolean if the field is editable. This option is used in cell, inline and form modules.
     */
    public $editable = false;
    /**
     * @var bolean if this column is hidden at initialization.
     */
    public $hidden = false;
    /**
     * @var bolean if this can be sorted.
     */
    public $sortable = true;
    /**
     * @var bolean if this option is false the title is not displayed in that column when we hover a cell with the mouse.
     */
    public $title = true;
    /**
     * @var number the initial width of the column, in pixels.
     */
    public $width = 150;
    /**
     * @var mixed predefined types (string) or custom function name that controls the format of this field.
     * See http://www.trirand.com/jqgridwiki/doku.php?id=wiki:predefined_formatter for more details.
     */
    public $formatter;
    /**
     * @var array format options can be defined for particular columns, overwriting the defaults from the language file.
     * See http://www.trirand.com/jqgridwiki/doku.php?id=wiki:predefined_formatter for more details.
     */
    public $formatoptions;

    /**
     * Constructor.
     * @param CGridView the grid view that owns this column.
     */
    public function __construct($grid) {
        $this->grid = $grid;
    }

    /**
     * Initializes the column.
     */
    public function init() {
        if ($this->name === null)
            throw new CException(Yii::t('jqgrid', '"name"  must be specified for JqGridDataColumn.'));
    }

    public function toArray() {
        foreach(get_object_vars($this) as $key=>$value){
            switch ($key) {
                case 'grid':
                    break;
                case 'formatter':
                    if ($this->formatter)$column['formatter'] = $this->formatter;
                    break;
                case 'formatoptions':
                    if(is_array($this->formatoptions))$column['formatoptions'] = $this->formatoptions;
                    break;
                default:
                    $options[$key] = $this->$key;
                    break;
            }
        }
        $column['index'] = $this->name;

        return $column;
    }

    /**
     * Renders the data cell content.
     * This method evaluates {@link value} or {@link name} and renders the result.
     * @param integer the row number (zero-based)
     * @param mixed the data associated with the row
     */
    public function getCellData($row) {
        $data = $this->grid->dataProvider->data[$row];
        if ($this->value !== null)
            $value = $this->evaluateExpression($this->value, array('data' => $data, 'row' => $row));
        else if ($this->name !== null)
            $value = CHtml::value($data, $this->name);
        return $value === null ? $this->grid->nullDisplay : $this->grid->getFormatter()->format($value, $this->type);
    }

}
